package com.cqc.processmanagerdemo;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Debug;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cui on 2017/8/2.
 */

public class ProcessUtil {


    public static int getProcessCount(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> proList = am.getRunningAppProcesses();
        return proList.size();
    }

    public static long getAvailMem(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(info);
        return info.availMem;
    }

    public static long getTotalMem(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(info);
        return info.totalMem;
    }

    /**
     * 获取所有进程信息
     */


    public static List<ProcessInfo> getProcessList(Context context) {
        List<ProcessInfo> list = new ArrayList<>();

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningAppProcesses) {
            String packageName = processInfo.processName;//一般情况下processName==PackageName
            Debug.MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{processInfo.pid});
            int totalPrivateDirty = processMemoryInfo[0].getTotalPrivateDirty();

            ProcessInfo info = new ProcessInfo();
            info.setPackageName(packageName);
            info.setMemorySize(totalPrivateDirty);
            info.setPid(processInfo.pid);

            PackageManager pm = context.getPackageManager();
            try {
                PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
                ApplicationInfo applicationInfo = packageInfo.applicationInfo;
                Drawable icon = applicationInfo.loadIcon(pm);
                String label = applicationInfo.loadLabel(pm).toString();

                info.setName(label);
                info.setIcon(icon);

                if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
                    info.setSystem(true);
                } else {
                    info.setSystem(false);
                }
            } catch (PackageManager.NameNotFoundException e) {//如果 processName != PackageName
                e.printStackTrace();
                info.setName(processInfo.processName);
                info.setIcon(context.getResources().getDrawable(R.mipmap.ic_launcher));
                info.setSystem(true);
            }
            list.add(info);
        }
        return list;
    }
}
